package com.lc.afbx.adapters;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.activity.PayWebActivity;
import com.lc.afbx.beans.DeleteIdType;
import com.lc.afbx.conn.GetApiMyOrder;
import com.lc.afbx.conn.GetApiMyOrderRecommend;
import com.lc.afbx.interf.BaoDanIdInterf;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/8.
 */
public class BaoDanAdapter extends BaseAdapter {
    private List businessList;
    private LayoutInflater inflater;
    private Context context;

    private BaoDanIdInterf baoDanIdInterf;


    private HashMap<Integer, DeleteIdType> DeleteIdMap;
    private HashMap<Integer, View> mView;
    public HashMap<Integer, Integer> visiblecheck;//用来记录是否显示checkBox
    public HashMap<Integer, Boolean> ischeck;
    private boolean isMulChoice;

    public BaoDanAdapter(Context context, List businessList, boolean b) {
        inflater = LayoutInflater.from(context);
        this.businessList = businessList;
        this.context = context;
        isMulChoice = b;


        DeleteIdMap = new HashMap<>();
        mView = new HashMap();
        visiblecheck = new HashMap();
        ischeck = new HashMap();


        if (isMulChoice) {
            for (int i = 0; i < businessList.size(); i++) {
                ischeck.put(i, false);
                visiblecheck.put(i, View.VISIBLE);
            }
        } else {
            for (int i = 0; i < businessList.size(); i++) {
                ischeck.put(i, false);
                visiblecheck.put(i, View.INVISIBLE);
            }
        }


    }


    @Override
    public int getCount() {

        DeleteIdMap.clear();
        return businessList.size();
    }

    @Override
    public Object getItem(int position) {
        return businessList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view = mView.get(position);

        if (view == null) {
            view = inflater.inflate(R.layout.item_myorder, null);
            BaseApplication.ScaleScreenHelper.loadView((ViewGroup) view);


            TextView baofei = (TextView) view.findViewById(R.id.baofei);
            TextView leixing = (TextView) view.findViewById(R.id.leixing);
            TextView time = (TextView) view.findViewById(R.id.time);
            TextView type_order = (TextView) view.findViewById(R.id.type_order);

            final CheckBox quanquan = (CheckBox) view.findViewById(R.id.quanquan);
            Button zhifu = (Button) view.findViewById(R.id.zhifu);


            //当修改选择时 支付按钮不设置点击事件
            if (isMulChoice == false) {

                zhifu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

//                        UtilToast.show(context, "支付界面还没写呢");

                        //  TODO 这里跳转支付页面
//                    Intent intent = new Intent(context, JARActivity.class);
                        Intent intent = new Intent(context, PayWebActivity.class);

                        if (businessList.get(position) instanceof GetApiMyOrder.Business) {
                            GetApiMyOrder.Business business = (GetApiMyOrder.Business) businessList.get(position);

                            intent.putExtra("type", business.type);
                            intent.putExtra("ordersn", business.ordersn);


                        } else if (businessList.get(position) instanceof GetApiMyOrder.Accident) {
                            GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) businessList.get(position);

                            intent.putExtra("type", accident.type);
                            intent.putExtra("ordersn", accident.ordersn);

                        }

                        context.startActivity(intent);
                    }
                });
            }

            quanquan.setChecked(ischeck.get(position));

            quanquan.setVisibility(visiblecheck.get(position) == 0 ? View.VISIBLE : View.INVISIBLE);

            //当修改时，item的点击事件是 选择radiobutton
            if (isMulChoice) {

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (!quanquan.isChecked()) {

                            UtilLog.e("点击方法", "看看");
                            quanquan.setChecked(true);

                            DeleteIdType d = new DeleteIdType();

                            //TODO  写到这了  单选添加List里

                            if (businessList.get(position) instanceof GetApiMyOrder.Business) {
                                GetApiMyOrder.Business business = (GetApiMyOrder.Business) businessList.get(position);

                                d.setId(business.id);
                                d.setType(business.type);

                                DeleteIdMap.put(position, d);

                            } else if (businessList.get(position) instanceof GetApiMyOrder.Accident) {
                                GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) businessList.get(position);

                                d.setId(accident.id);
                                d.setType(accident.type);
                                DeleteIdMap.put(position, d);
                            }

                        } else {
                            UtilLog.e("点击方法", "取消选择");
                            quanquan.setChecked(false);
                            DeleteIdMap.remove(position);

                        }

                    }
                });

            }

//            quanquan.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    if (isMulChoice) {
//                        if (quanquan.isChecked()) {
//
//                            UtilLog.e("点击方法", "看看");
//                            quanquan.setChecked(true);
//
//                            DeleteIdType d = new DeleteIdType();
//
//                            //TODO  写到这了  单选添加List里
//
//                            if (businessList.get(position) instanceof GetApiMyOrder.Business) {
//                                GetApiMyOrder.Business business = (GetApiMyOrder.Business) businessList.get(position);
//
//                                d.setId(business.id);
//                                d.setType(business.type);
//
//                                DeleteIdMap.put(position, d);
//
//                            } else if (businessList.get(position) instanceof GetApiMyOrder.Accident) {
//                                GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) businessList.get(position);
//
//                                d.setId(accident.id);
//                                d.setType(accident.type);
//                                DeleteIdMap.put(position, d);
//                            }
//
//                        } else {
//                            UtilLog.e("点击方法", "取消选择");
//                            quanquan.setChecked(false);
//                            DeleteIdMap.remove(position);
//
//                        }
//                    } else {
////                        Toast.makeText(context, "点击了", Toast.LENGTH_LONG).show();
//                    }
//
//                }
//            });


//        "id":"1","posttime":"2016-06-08 09:15:25","total":"5000.00","pstate":"1","title":"\u5546\u4e1a\u9669"

            // 车险商险
            if (businessList.get(position) instanceof GetApiMyOrder.Business) {

                baoDanIdInterf = (BaoDanIdInterf) context;

                GetApiMyOrder.Business business = (GetApiMyOrder.Business) businessList.get(position);
                time.setText(business.posttime);
                leixing.setText(business.title);
                baofei.setText(business.total);

                if (business.pstate.equals("2")) {
                    type_order.setTextColor(context.getResources().getColor(R.color.order1));
                    type_order.setText("已承保");
                    zhifu.setVisibility(View.GONE);
                } else {
                    type_order.setTextColor(context.getResources().getColor(R.color.order2));
                    type_order.setText("待支付");
                    zhifu.setVisibility(View.VISIBLE);

                }

            } else if (businessList.get(position) instanceof GetApiMyOrder.Accident) {

                baoDanIdInterf = (BaoDanIdInterf) context;

                GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) businessList.get(position);
                time.setText(accident.posttime);
                leixing.setText(accident.insurance_type);
                baofei.setText(accident.premium);

                if (accident.type.equals("4")) {

                    zhifu.setVisibility(View.GONE);
//                    type_order.setVisibility(View.GONE);

                    if (accident.pstate.equals("2")) {
                        type_order.setTextColor(context.getResources().getColor(R.color.order1));
                        type_order.setText("已承保");
//                        zhifu.setVisibility(View.GONE);
                    } else {
                        type_order.setTextColor(context.getResources().getColor(R.color.order2));
                        type_order.setText("待支付");
//                        zhifu.setVisibility(View.VISIBLE);

                    }

                } else {

                    if (accident.pstate.equals("2")) {
                        type_order.setTextColor(context.getResources().getColor(R.color.order1));
                        type_order.setText("已承保");
                        zhifu.setVisibility(View.GONE);
                    } else {
                        type_order.setTextColor(context.getResources().getColor(R.color.order2));
                        type_order.setText("待支付");
                        zhifu.setVisibility(View.VISIBLE);

                    }
                }


            } else {
                GetApiMyOrderRecommend.Data data = (GetApiMyOrderRecommend.Data) businessList.get(position);

                time.setText(data.posttime);

                if (TextUtils.isEmpty(data.insurance_type)) {
                    leixing.setText(data.title);
                } else {
                    leixing.setText(data.insurance_type);
                }

                if (TextUtils.isEmpty(data.premium)) {
                    baofei.setText(data.total);
                } else {
                    baofei.setText(data.premium);
                }


                if (data.pstate.equals("2")) {
                    type_order.setTextColor(context.getResources().getColor(R.color.order1));
                    type_order.setText("已承保");
                    zhifu.setVisibility(View.GONE);
                } else {
                    type_order.setTextColor(context.getResources().getColor(R.color.order2));
                    type_order.setText("待支付");
                    zhifu.setVisibility(View.GONE);
                }

            }
            mView.put(position, view);
        }


        return view;
    }

    public void doCallback() {

        if (DeleteIdMap.size() > 0) {
            JSONArray array = new JSONArray();

            for (Integer key : DeleteIdMap.keySet()) {

                System.out.println("key= " + key + " and value= " + DeleteIdMap.get(key));

                String str = DeleteIdMap.get(key).toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                array.put(jsonObject);
            }

            Log.e("这是保单adapter", array.toString());

            baoDanIdInterf.getStringBaoDanId(array);
        } else {
            baoDanIdInterf.getStringBaoDanId(null);
        }
    }

}
